'use strict';

const Sequelize = require('sequelize');
const sequelize = require('../plugins/db');

const Model = Sequelize.Model;
class Page extends Model {}

// 静态属性：主键字段名称
Page.pk = 'id';
Page.init({
    id: {
      type: Sequelize.INTEGER,
      autoIncrement: true,
      primaryKey: true,
      comment: '页面id'
    },
    title: {
      type:  Sequelize.STRING,
      allowNull: false,
      comment: '标题'
    },
    custom_path: {
      type: Sequelize.STRING,
      comment: '自定义path'
    },
    content: {
      type: Sequelize.TEXT,
      comment: '页面内容'
    },
    images: {
      type: Sequelize.TEXT,
      comment: '图片数组',
      allowNull: true,
      set: function(val) {
          if (val){
          this.setDataValue('images', val.join(','));
          }else {
              return null;
          }
      },
      get() {
          const images = this.getDataValue('images');
          return images ? images.split(',') : null
      }
  },
    cover: {
      type: Sequelize.TEXT,
      comment: '封面图片'
    },
    status: {
      type: Sequelize.STRING,
      defaultValue: 'normal',
      comment: '状态'
    },
    comment: {
      type:  Sequelize.ENUM('allow', 'disallow'),
      defaultValue: 'disallow',
      comment: '评论状态'
    }
}, {
    sequelize,
    tableName: 'page',
    freezeTableName: true,
    timestamps: true
});
module.exports = Page;
